package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.mapper.TravelCommendMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelCommendQuery;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TravelCommendServiceImpl implements ITravelCommendService {
    @Autowired
    private TravelCommendMapper travelCommendMapper;

    /**
     * 查询推荐游记
     * @param qo 查询条件
     * @return
     */
    @Override
    public PageInfo<TravelCommend> query(TravelCommendQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<TravelCommend> commends = travelCommendMapper.selectForList(qo);
        return new PageInfo<>(commends);
    }

    @Override
    public void saveOrUpdate(TravelCommend commend, MultipartFile file) {
        if (file != null && file.getSize() > 0) {
            String fileName = UploadUtil.uploadQiniu(file);
            commend.setCoverUrl(fileName);
        }
        if (commend.getId() != null) {
            travelCommendMapper.updateByPrimaryKey(commend);
        }else {
            travelCommendMapper.insert(commend);
        }
    }

    @Override
    public PageInfo<Map> queryForApp(TravelCommendQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Map> travelCommends = travelCommendMapper.selectForAppList(qo);
        return new PageInfo<>(travelCommends);
    }
}
